package fgame.action
{
	
	import fgame.GameState;
	import fgame.unit.Actor3D;
	
	import flare.core.Pivot3D;
	import flare.materials.Material3D;
	import flare.materials.Shader3D;
	import flare.materials.filters.ColorFilter;
	import flare.primitives.Box;
	
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	import ricky.game.action.Act;
	import ricky.utils.FrameManager;
	import ricky.utils.TimeServer;
	
	/**
	 * 移动行动
	 */
	public class MoveAct extends Act3D
	{
		public function MoveAct(actor:Actor3D=null)
		{
			super(actor);
			_types=1;

			this.animProperty="walk";
			_target=new  Point();
			_myPos=new  Point();
			_p1=new Vector3D();
			_rotation=new Vector3D();
			_pviot=new Pivot3D();
			
			//
			testBox=new Box("",20,20,20);
			var material5:Shader3D = new Shader3D( "test5" );
			material5.filters.push(new ColorFilter(0xff0000));
			testBox.setMaterial(material5);
		}
		
		override public function begin():void
		{

			this._actor.playAnimAct(animProperty);

			super.begin();
		}
		
		override public function action(time:Number):void
		{
			var dot:Number=time-_lastFrameTime;
			var tfps:Number=16.6;
			_lastFrameTime=time;
			_fpsDt+=dot;
			if(_fpsDt>1000)
			{
				_fpsDt=0;
				var fps:int=FrameManager.inst.fps;
				tfps=1000/fps;
//				lookAtTarget();
			}
			
			if(_speed!=currActor.speed && time-_beginTime>=_clock) //追赶上了速度回原
			{
				this.currActor.speed=_speed;
			}
			
			if(_speed!=currActor.speed && time-_beginTime>_clock)
			{
				currActor.speed=_speed;
			}
			var t:Number=currActor.speed*tfps;
			//				Debug.traceMe(t,"距离"+dot+"speed"+currActor.speed);
			this.graphic.translateZ(t);
			super.setToSurface();
			
			if(_state==3)// && this.isArrive())
			{
				this.setMyPos();
//				if(isArrive())
					this.complete();
//				else
//				{
//					Debug.traceMe("没到达！"+isArrive()+this._target+"我的位置"+this._myPos);
//				}
			}
		}
		
		private var testBox:Box;
		
		override public function purpose(args:*):void
		{
			_speed=args.v;//目标运行速度 0.1
			
			_state=args.state;//状态
			_p1.x=args.p.x;_p1.y=args.p.y;_p1.z=args.p.z;//原始位置
			_rotation.x=args.r.x;_rotation.y=args.r.y;_rotation.z=args.r.z;//原始旋转
			_clock=args.clock;//同步时间间隔
			var dt:Number=this.currActor.netDelay;

			//预测位置  注意坐标系
			_pviot.x=_p1.x;_pviot.y=_p1.y;_pviot.z=_p1.z;
//			Debug.traceMe(_pviot.z,"z "+"传送："+_speed*_clock);
			_pviot.setRotation(_rotation.x,_rotation.y,_rotation.z);
			if(_speed==0)
			{
				_pviot.translateZ(this.currActor.speed*dt);
			}
			else
			{
				_pviot.translateZ(_speed*_clock);
			}
//			Debug.traceMe(_pviot.z,"translateZ ");

			_target.x=_pviot.x;
			_target.y=_pviot.z;

			testBox.parent=GameState.inst.sceneManager.scene;
			testBox.setPosition(_pviot.x,_pviot.y,_pviot.z);
			
			if(_speed!=currActor.speed)
			{
				this._beginTime=TimeServer.now();
				setMyPos();
				var sMe:Number=_target.subtract(_myPos).length;//我移动位移
				var v:Number=-sMe/(_clock-dt);
				if(v>4*_speed)
					v=4*_speed; 
				currActor.speed=v;
			}
			
			if(_state<3)
				lookAtTarget();
			else if(_state==3)
			{
				
			}
		
				
			if(_state==0)//开始
			{	
				
			}else if(_state==1)//同步
			{
				
				
			}else if(_state==2)//速度角度变化
			{
				
			}else  //停止
			{
				
			}

		}
		
		/**
		 * 是否移动到目标x,y
		 */
		protected function isArrive():Boolean
		{
			if (_target.subtract(_myPos).length<_radius)//if(x==this._currentObject.coordinate.x && this._currentObject.coordinate.y==y)
				return true;
			else
				return false;
		}
		
		protected function lookAtTarget():void
		{
			graphic.lookAt(_pviot.x,_pviot.y,_pviot.z);
			graphic.rotateY(180);
		}
		
		protected function setMyPos():void
		{
			_myPos.x=_actor.graphic.x;
			_myPos.y=_actor.graphic.z;
		}
			
		/** 上一帧时间 */
		protected var _lastFrameTime:Number=0;
		protected var _fpsDt:Number;
		protected var _radius:Number=5;
		
		private var _myPos:Point;
		private var _target:Point;
		
		/** 旋转角度 */
		private var _rotation:Vector3D;
		/** 当时位置  */
		private var _p1:Vector3D;
		
		/** 状态  */
		private var _state:int;
		/**  目标速度 */
		protected var _speed:Number=-0.10;
		/** 同步时钟毫秒  */
		private var _clock:int=500;
		/**  用来计算位置的对象 */
		private var _pviot:Pivot3D;
		/**  加速度  */
		private var _g:Number;

	}
}